package com.camps.sdk.decrypt;

import com.camps.sdk.common.Constants;
import com.camps.sdk.entity.DeviceUpMessage;
import com.camps.sdk.entity.LocationData;
import com.camps.sdk.entity.SOSWarning;
import com.camps.sdk.utils.LocationUtils;

import java.util.List;

/**
 * @Author ldaoliang
 * @Date create in 15:58 2021/9/1
 * @Description
 */
public class AnalyzeSosWarn implements AnalyticalData {
    @Override
    public DeviceUpMessage parseData(String originalData)
    {
        // *WT,866248050395985,SOS,100444,V,0.0000,S,0.0000,W,0.00,0,460,1,102264330,9519,5,0c:4b:54:3e:6d:5b,-43,3c:46:d8:87:96:f1,-54,c2:d5:4d:3b:2c:03,-68,28:94:0f:f9:c4:85,-68,38:83:45:cc:7d:62,-85,040621,FFDFFFFF#
        //解密
        String[] split = parseCommand(originalData);
        String imei = split[1];
        String commandNumber = split[3];
        String lat = split[5];
        String lng = split[7];
        String speed = split[9];
        String orientation = split[10];
        String mcc = split[11];//移动设备国家代码
        String nid = split[12];//网络识别码
        String sid = split[13];//系统识别码
        String cellId = split[14];
        int wifiNum = Integer.parseInt(split[15]);
        String wifiInfo = "";
        //wifi信息:mac地址,信号强度,wifi名称|mac地址,信号强度,wifi名称
        for (int i = 0; i < wifiNum; i++) {
            if(wifiInfo !=null && !wifiInfo.equals("")){
                wifiInfo =wifiInfo.concat("|");
            }
            wifiInfo =wifiInfo.concat(split[16 + 2*i]).concat(",").concat(split[17 + 2*i]);
        }
        String date = split[split.length-2];//日月年
        String deviceStatus = split[split.length-1];
        SOSWarning sosWarning = new SOSWarning();
        sosWarning.setCommandNumber(commandNumber);
        sosWarning.setImei(imei);
        sosWarning.setDeviceDate(date);
        sosWarning.setDeviceTime(commandNumber);
        sosWarning.setWifiInfo(wifiInfo);
        sosWarning.setDeviceStatus(deviceStatus);
        sosWarning.setMcc(mcc);
        sosWarning.setNid(nid);
        sosWarning.setSid(sid);
        LocationData locationData = new LocationData();
        List<Double> list = LocationUtils.transiformLatAndLng(lng, lat);
        locationData.setOrientation(Integer.parseInt(orientation));
        locationData.setSpeed(Double.parseDouble(speed));
        locationData.setLatitude(list.get(1));
        locationData.setLongitude(list.get(0));
        sosWarning.setLocationData(locationData);
        return structureMessage(Constants.UPLOAD,Constants.UP_SOS_WARNING,false,"",sosWarning);
    }
}
